<?php
class ormwhere{
    private static $instance;//实例寄存器
    
    private static $modelName;//模型名
    
    private static $options;//条件数组
    
    private static $object;//ORM对象寄存器
    
    private static $relation;//关系表连接
    
    private static $order_by;//排列
    
    //private static $modified = false;
    
    public static function instance($modelName = null){
        if (is_string($modelName)){
            self::$modelName = $modelName;
        }
        self::$options = array();
        return self::$instance = new self;
    }
    
    public function model($modelName){
        if (is_string($modelName)){
            self::$modelName = $modelName;
            self::$object = null;
        }else{
            die('Param is not a string');
        }
        return self::$instance;
    }
    
    public function object($object){
        if (is_object($object)){
            self::$object = $object;
            //self::$modelName = strtolower(str_ireplace('model_','',get_class($object)));
        }else{
            die('Param is not a object');
        }
        return self::$instance;
    }
    
    public function relation($relation){
        if (!is_string(self::$modelName) && !is_object(self::$object)){
            die('Neither the modelName or the object have set up');
        }
        if (is_string($relation)){
            self::$relation = $relation;
        }else{
            die('Param is not a string');
        }
        return self::$instance;
    }
    
    public function order_by($column,$direction='ASC'){
        if (!is_string(self::$modelName) && !is_object(self::$object)){
            die('Neither the modelName or the object have set up');
        }
        if (is_string($column)){
            self::$order_by = array($column,$direction);
        }else{
            die('Param is not a string');
        }
        return self::$instance;
    }
    
    //example:where(array(array('userId','=',1),array('id','>',5)))
    public function where(array $options,$add = false){
        if (!$add) self::$options = array();
        if (is_array($options)){
            foreach ($options as $option){
                self::$options[] = $option;
            }
        }else{
            die('Condition is not an array');
        }
        return self::$instance;
    }
    
    //like orm where
    public function add_where($key,$compare,$value){
        if ($key && $compare){
            self::$options[] = func_get_args();
        }else{
            die('meet error on wrong conditions');
        }
        return self::$instance;
    }
    
    public function combine($model){
        //关联表
        if (is_string(self::$relation)){
            $model = $model->{self::$relation};
        }
        //排列
        if (is_array(self::$order_by)){
            $model = $model->order_by(self::$order_by[0],self::$order_by[1]);
        }
        foreach (self::$options as $option){
            $model->where($option[0],$option[1],$option[2]);
        }
        return $model;
    }
    
    public function getobject(){
        if (empty(self::$object) && empty(self::$modelName)){
            die('object have not definition');
        }
        if (is_string(self::$modelName)){
            if (is_object(self::$object)){
                self::$object->clear();
            }else{
                self::$object = ORM::factory(self::$modelName);
            }
        }
        
        return $this->combine(self::$object);
    }
    
    public function getORMobject(){
        return self::$object;
    }
}